import java.util.*;
import java.sql.*;

public class MoreInfo {

	public static void moreInfo() throws SQLException, ClassNotFoundException {
		Scanner input = new Scanner(System.in);
		String s = null;
		int ItemID = 0;
		int i = 0;
		ResultSet item = null;
		ResultSet r1 = null;

		//Connect to Database
		Connection c = mysqlConnection.connect();

		System.out.println("\nEnter the ItemID of the item you would like to see: ");
		s = input.nextLine();

		//Check Item ID
		while (item == null || !(item.next())) {
			//Is ItemID int?
			
			if (isInteger(s)) {
				ItemID = Integer.parseInt(s);
			
				PreparedStatement findItem = c.prepareStatement("SELECT * FROM TradeItem WHERE ItemID = ?");
				findItem.setInt(1, ItemID);
				item = findItem.executeQuery();
				if (!(item.next())) {
					System.out.println("\nSorry, the item you entered could not be found. Please try again: ");
					s = input.nextLine();
				}
				else {
					break;
				}	
			}
			else {
				System.out.println("\nError: ItemID must be a number.");
				System.out.println("Please try again: ");
				s = input.nextLine();
			}
		}
	
		System.out.println("\nItem ID: " + ItemID);

		//Item Condition
		item.first();
		s = item.getString(2);
		System.out.println("Item Condition: " + s);

		//Time Left Not sure what to do here what is "Time Left" - Time? Timestamp? Date? So haven't implemented yet.

		//Image Path
		s = item.getString(4);
		if (s == null)
			System.out.println("No image.");
		else
			System.out.println("Image: " + s);
		
		//Description
		s = item.getString(5);
		System.out.println("Description: " + s);
		
		//Trade Location
		s = item.getString(6);
		System.out.println("Trade Location: " + s);
		
		//Category
		i = item.getInt(7);

		PreparedStatement catName = c.prepareStatement("SELECT Name FROM Categories WHERE CategoryID = ?");
		catName.setInt(1, i);
		r1 = catName.executeQuery();
		r1.first();
		s = r1.getString(1);
		System.out.println("Category: " + s);
		
		//Owner
		i = item.getInt(8);

		PreparedStatement ownName = c.prepareStatement("SELECT Username FROM User WHERE UserID + ?");
		ownName.setInt(1, i);
		r1 = ownName.executeQuery();
		r1.first();
		s = r1.getString(1);
		System.out.println("Owner Username: " + s);
		
		//Moderator
		//i = item.getInt(9);		

		//if (!(i > 0))
		//	System.out.println("Not approved by a moderator.");
		//else
		//	System.out.println("Approved by Moderator ID " + i);		
	}
	
	public static boolean isInteger( String input ) {
    		try {
      		  Integer.parseInt( input );
        		return true;
   		 }
    		catch( Exception e ) {
        		return false;
    		}
	}
}
		

